package com.weilive.core.netty.spring;

import com.weilive.core.netty.NettyServer;
import com.weilive.core.netty.NettyServerConfig;
import com.weilive.core.utils.StringUtils;

public class NettySpringServer {

	private String serverClassName;
	
	private String port;
	private String corePoolSize;
	private String maximumPoolSize;
	private String keepAliveSeconds;
	private String workerCount;
	
	public String getServerClassName() {
		return serverClassName;
	}
	
	public void setServerClassName(String serverClassName) {
		this.serverClassName = serverClassName;
	}
	
	public String getPort() {
		return port;
	}
	
	public void setPort(String port) {
		this.port = port;
	}

	public String getCorePoolSize() {
		return corePoolSize;
	}

	public void setCorePoolSize(String corePoolSize) {
		this.corePoolSize = corePoolSize;
	}

	public String getMaximumPoolSize() {
		return maximumPoolSize;
	}

	public void setMaximumPoolSize(String maximumPoolSize) {
		this.maximumPoolSize = maximumPoolSize;
	}

	public String getKeepAliveSeconds() {
		return keepAliveSeconds;
	}

	public void setKeepAliveSeconds(String keepAliveSeconds) {
		this.keepAliveSeconds = keepAliveSeconds;
	}

	public String getWorkerCount() {
		return workerCount;
	}

	public void setWorkerCount(String workerCount) {
		this.workerCount = workerCount;
	}
	
	public NettyServer getServer() throws Exception {
		NettyServerConfig config = new NettyServerConfig();
		config.setPort(StringUtils.parseInt(port, NettyServerConfig.DEFAULT_PORT));
		config.setCorePoolSize(StringUtils.parseInt(corePoolSize, NettyServerConfig.DEFAULT_CORE_POOL_SIZE));
		config.setMaximumPoolSize(StringUtils.parseInt(maximumPoolSize, NettyServerConfig.DEFAULT_MAX_POOL_SIZE));
		config.setUnit(NettyServerConfig.DEFAULT_UNIT);
		config.setKeepAliveTime(StringUtils.parseLong(keepAliveSeconds, NettyServerConfig.DEFAULT_KEEP_ALIVE_TIME));
		config.setWorkerCount(StringUtils.parseInt(workerCount, NettyServerConfig.DEFAULT_WORKER_COUNT));
		
		NettyServer server = (NettyServer)Class.forName(serverClassName).newInstance();
		server.setConfig(config);
		return server;
	}
}
